package com.ruoyi.web.controller.tool;

import cn.hutool.core.lang.Validator;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.LocationUtils;
import com.ruoyi.system.basicInformation.bo.TTerminalTrajectoryQueryBo;
import com.ruoyi.system.basicInformation.service.ITTerminalTrajectoryService;
import com.ruoyi.system.basicInformation.vo.TTerminalTrajectoryVo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * swagger 用户测试方法
 * 
 * @author ruoyi
 */
@Api("用户信息管理")
@RestController
@RequestMapping("/test/user")
public class TestController extends BaseController
{
    @Autowired
    private ITTerminalTrajectoryService iTTerminalTrajectoryService;
    private final static Map<Integer, UserEntity> users = new LinkedHashMap<Integer, UserEntity>();
    {
        users.put(1, new UserEntity(1, "admin", "admin123", "15888888888"));
        users.put(2, new UserEntity(2, "ry", "admin123", "15666666666"));
    }

    @ApiOperation("获取用户列表")
    @GetMapping("/list")
    public AjaxResult userList()
    {
        List<UserEntity> userList = new ArrayList<UserEntity>(users.values());
        return AjaxResult.success(userList);
    }

    @ApiOperation("获取最终")
    @GetMapping("/getTerminalTrajectoryVos")
    public AjaxResult getTerminalTrajectoryVos()
    {
        SimpleDateFormat simpleDateFormat= new SimpleDateFormat();
        TTerminalTrajectoryQueryBo queryBo = new TTerminalTrajectoryQueryBo();
        try {
            queryBo.setBeginDate(simpleDateFormat.parse("2022-04-26 13:47:02"));
            queryBo.setOverDate(simpleDateFormat.parse("2022-04-26 13:53:33"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        queryBo.setTerminalId((long) 15);
        queryBo.setTerminalType("1");
        List<TTerminalTrajectoryVo> tTerminalTrajectoryVos = iTTerminalTrajectoryService.queryList(queryBo);

        if (tTerminalTrajectoryVos != null && tTerminalTrajectoryVos.size()>1) {
            double mileage = 0f;
            for (Integer i = 0, j = 1; i < tTerminalTrajectoryVos.size(); i++, j++) {
                if(j<tTerminalTrajectoryVos.size()-1){
                    mileage += LocationUtils.getDistance(
                            tTerminalTrajectoryVos.get(i).getX().doubleValue(),
                            tTerminalTrajectoryVos.get(i).getY().doubleValue(),
                            tTerminalTrajectoryVos.get(j).getX().doubleValue(),
                            tTerminalTrajectoryVos.get(j).getY().doubleValue()
                    );
                }
            }
            return AjaxResult.success(mileage);
        }
        return AjaxResult.success();
    }




    @ApiOperation("获取用户详细")
    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path")
    @GetMapping("/{userId}")
    public AjaxResult getUser(@PathVariable Integer userId)
    {
        if (!users.isEmpty() && users.containsKey(userId))
        {
            return AjaxResult.success(users.get(userId));
        }
        else
        {
            return AjaxResult.error("用户不存在");
        }
    }

    @ApiOperation("新增用户")
    @ApiImplicitParam(name = "userEntity", value = "新增用户信息", dataType = "UserEntity")
    @PostMapping("/save")
    public AjaxResult save(UserEntity user)
    {
        if (Validator.isNull(user) || Validator.isNull(user.getUserId()))
        {
            return AjaxResult.error("用户ID不能为空");
        }
        return AjaxResult.success(users.put(user.getUserId(), user));
    }

    @ApiOperation("更新用户")
    @ApiImplicitParam(name = "userEntity", value = "新增用户信息", dataType = "UserEntity")
    @PutMapping("/update")
    public AjaxResult update(UserEntity user)
    {
        if (Validator.isNull(user) || Validator.isNull(user.getUserId()))
        {
            return AjaxResult.error("用户ID不能为空");
        }
        if (users.isEmpty() || !users.containsKey(user.getUserId()))
        {
            return AjaxResult.error("用户不存在");
        }
        users.remove(user.getUserId());
        return AjaxResult.success(users.put(user.getUserId(), user));
    }

    @ApiOperation("删除用户信息")
    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path")
    @DeleteMapping("/{userId}")
    public AjaxResult delete(@PathVariable Integer userId)
    {
        if (!users.isEmpty() && users.containsKey(userId))
        {
            users.remove(userId);
            return AjaxResult.success();
        }
        else
        {
            return AjaxResult.error("用户不存在");
        }
    }
}

@ApiModel("用户实体")
class UserEntity
{
    @ApiModelProperty("用户ID")
    private Integer userId;

    @ApiModelProperty("用户名称")
    private String username;

    @ApiModelProperty("用户密码")
    private String password;

    @ApiModelProperty("用户手机")
    private String mobile;

    public UserEntity()
    {

    }

    public UserEntity(Integer userId, String username, String password, String mobile)
    {
        this.userId = userId;
        this.username = username;
        this.password = password;
        this.mobile = mobile;
    }

    public Integer getUserId()
    {
        return userId;
    }

    public void setUserId(Integer userId)
    {
        this.userId = userId;
    }

    public String getUsername()
    {
        return username;
    }

    public void setUsername(String username)
    {
        this.username = username;
    }

    public String getPassword()
    {
        return password;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

    public String getMobile()
    {
        return mobile;
    }

    public void setMobile(String mobile)
    {
        this.mobile = mobile;
    }
}
